package ua.races.command.implementation;

import org.apache.log4j.Logger;
import ua.races.command.ServletCommand;
import ua.races.dao.RacesDaoImpl;
import ua.races.relultObjects.FullRace;
import ua.races.vo.BetType;
import ua.races.vo.Race;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AddBetToDbCommand implements ServletCommand {
    private static final Logger log = Logger.getLogger(StaffCheckCommand.class.getName());

    public void execute(HttpServletRequest req, HttpServletResponse resp, ServletContext context) {

        RacesDaoImpl dao;
        dao = (RacesDaoImpl) context.getAttribute("DAO");
        Race race = null;
        BetType betType =  null;
        List<FullRace> fullRace = null;
        List<Race> races = null;

        try {
            race = dao.findRaceById(Long.parseLong(req.getParameter("raceId")));
            betType = dao.findBetTypeById(Long.parseLong(req.getParameter("betTypeId")));
            races = new ArrayList<Race>();
            races.add(race);
            fullRace = dao.getFullRaces(races);
        } catch (SQLException e) {
            log.error(e);
        }

        req.setAttribute("fullRace",fullRace.get(0));
        req.setAttribute("betType",betType);

        try {
            context.getRequestDispatcher("/addNewBet.jsp").forward(req, resp);
        } catch (ServletException e) {
            log.error(e);
        } catch (IOException e) {
            log.error(e);
        }

    }
}
